#include "StreamSessionWidget.h"

int timeFromNow(char *szTime)
{
	QDateTime gmtDateTime = QDateTime::currentDateTime().toTimeSpec(Qt::UTC);
	QDateTime estDateTime = gmtDateTime.addSecs(-(5*60*60));

	QDateTime datetime = QDateTime::fromString(szTime, SETTINGS_TIME_FORMAT);
	int diff = estDateTime.secsTo(datetime);

	return diff > 0 ? diff : 0;
}

StreamSessionWidget::StreamSessionWidget(void) : m_availSlot(0), m_nextSlot(0)
{
	createInterface();
	connect(m_cmbCurrentSessionContent, SIGNAL(currentIndexChanged(int)),
		this, SLOT(onSelectedIndexChanged(int)));

	m_timer = new QTimer();
	m_timer->setInterval(1000);
	connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimer()));
	m_timer->start();
}

StreamSessionWidget::~StreamSessionWidget(void)
{
	while (m_availSlot)
	{
		IceCastSlot *tmp = m_availSlot;
		m_availSlot = m_availSlot->next;
		DEL_SLOT_INFO(tmp);
	}
}

void StreamSessionWidget::createInterface()
{
	setTitle(tr(TITLE_SECTION_SESSION));

	m_lblCurrentSession = new QLabel(tr(LABEL_CURRENT_SESSION));
	m_lblNextSession = new QLabel(tr(LABEL_NEXT_SESSION));
	m_lblBeforeSessionStart = new QLabel(tr(LABEL_BEFORE_SESSION_START));
	m_lblBeforeSessionEnd = new QLabel(tr(LABEL_BEFORE_SESSION_END));
	m_cmbCurrentSessionContent = new QComboBox();
	m_cmbNextSessionContent = new QComboBox();
	m_lblBeforeSessionStartContent = new QLabel();
	m_lblBeforeSessionEndContent = new QLabel();
	m_cmbCurrentSessionContent->setFixedWidth(240);
	m_cmbNextSessionContent->setFixedWidth(240);
	m_cmbNextSessionContent->setEnabled(false);

	m_lblBeforeSessionStartContent->setStyleSheet("QLabel {padding: 1px 0px 1px 0px; font:new courier; font-weight: bold; background-color : #101010; color : gray; font-size: 28px;}");
	m_lblBeforeSessionStartContent->setText(tr(LABEL_TIME_ZERO));
	m_lblBeforeSessionEndContent->setStyleSheet("QLabel {padding: 1px 0px 1px 0px; font: new courier; font-weight: bold; background-color : #101010; color : gray; font-size: 28px;}");
	m_lblBeforeSessionEndContent->setText(tr(LABEL_TIME_ZERO));

	QGridLayout *subLayout = new QGridLayout();
	subLayout->addWidget(m_lblBeforeSessionStartContent, 0, 0, Qt::AlignCenter);
	subLayout->addWidget(m_lblBeforeSessionEndContent, 0, 1, Qt::AlignCenter);
	subLayout->addWidget(m_lblBeforeSessionStart, 1, 0, Qt::AlignCenter);
	subLayout->addWidget(m_lblBeforeSessionEnd, 1, 1, Qt::AlignCenter);

	QGridLayout *grdLayout = new QGridLayout();
	grdLayout->addWidget(m_lblCurrentSession, 0, 0, Qt::AlignLeft);
	grdLayout->addWidget(m_cmbCurrentSessionContent, 0, 1, Qt::AlignLeft);
	grdLayout->addWidget(m_lblNextSession, 1, 0, Qt::AlignLeft);
	grdLayout->addWidget(m_cmbNextSessionContent, 1, 1, Qt::AlignLeft);
	grdLayout->addLayout(subLayout, 2, 0, 1, 2);

#if !defined(_WIN32) && !defined(_WIN64)
	grdLayout->setContentsMargins(0, 0, 0, 0);
#endif
	setLayout(grdLayout);
}

void StreamSessionWidget::update(const IceCastSlot *slotAvail, const IceCastSlot *slotNext)
{
	while (m_availSlot)
	{
		IceCastSlot *tmp = m_availSlot;
		m_availSlot = m_availSlot->next;
		DEL_SLOT_INFO(tmp);
	}

	m_availSlot = (IceCastSlot *)slotAvail;
	m_nextSlot = (IceCastSlot *)slotNext;

	// update available slots
	m_cmbCurrentSessionContent->clear();
#if TESTMODE
	m_cmbCurrentSessionContent->addItem("Test slot", 0);
#else
	m_cmbCurrentSessionContent->addItem("None", 0);
#endif
	IceCastSlot * tmp = m_availSlot;
	while (0 != tmp)
	{
		QDateTime timestart = QDateTime::fromString(tmp->timeStart, SETTINGS_TIME_FORMAT);
		QDateTime timeend = QDateTime::fromString(tmp->timeEnd, SETTINGS_TIME_FORMAT);
		m_cmbCurrentSessionContent->addItem(QString() + tmp->customer_name + " (" +
				tmp->custLocation + ") " +
				timestart.toString("yyyy-MM-dd") + " [" +
				timestart.toString("hh") + "-" +
				timeend.toString("hh") + "]", tmp->id);
		tmp = tmp->next;
	}

	// update next slot
	m_cmbNextSessionContent->clear();
	if (0 != m_nextSlot)
	{
		QDateTime timestart = QDateTime::fromString(m_nextSlot->timeStart, SETTINGS_TIME_FORMAT);
		QDateTime timeend = QDateTime::fromString(m_nextSlot->timeEnd, SETTINGS_TIME_FORMAT);
		m_cmbNextSessionContent->addItem(QString() + m_nextSlot->customer_name + " (" +
			m_nextSlot->custLocation + ") " +
			timestart.toString("yyyy-MM-dd") + " [" +
			timestart.toString("hh") + "-" +
			timeend.toString("hh") + "]", m_nextSlot->id);
	}
	else
		m_cmbNextSessionContent->addItem(tr(LABEL_NOT_AVAIL), 0);

	onTimer();
}

void StreamSessionWidget::onTimer()
{
	if (0 == m_availSlot && 0 == m_nextSlot)
		return;

	char szBuffer[MAX_LEN];
	IceCastSlot *slot = currentSlot();

	if (!slot)
	{
		m_lblBeforeSessionStartContent->setText(tr(LABEL_TIME_ZERO));
		m_lblBeforeSessionEndContent->setText(tr(LABEL_TIME_ZERO));
		return;
	}

	int t1 = timeFromNow(slot->timeStart);
	if (t1 > 0)
	{
		int m1 = (t1 % 3600) / 60, s1 = t1 % 60;
		sprintf_s(szBuffer, MAX_LEN, "%.2d:%.2d", m1, s1);
		m_lblBeforeSessionStartContent->setText(tr(szBuffer));
	}
	else
		m_lblBeforeSessionStartContent->setText(tr(LABEL_TIME_ZERO));

	int t2 = timeFromNow(slot->timeEnd);
	if (t2 > 0)
	{
		int m2 = (t2 % 3600) / 60, s2 = t2 % 60;
		sprintf_s(szBuffer, MAX_LEN, "%.2d:%.2d", m2, s2);
		m_lblBeforeSessionEndContent->setText(tr(szBuffer));
	}
	else
		m_lblBeforeSessionEndContent->setText(tr(LABEL_TIME_ZERO));
}

void StreamSessionWidget::onSelectedIndexChanged(int index)
{
	onTimer();
	IceCastSlot *slot = currentSlot();

	onCurrentSlotChanged(slot);
}

IceCastSlot * StreamSessionWidget::currentSlot()
{
	int id = m_cmbCurrentSessionContent->itemData(m_cmbCurrentSessionContent->currentIndex()).toInt();
	IceCastSlot *slot = m_availSlot;
	while (slot)
	{
		if (slot->id == id)
			break;
		slot = slot->next;
	}

#if TESTMODE
	if (!slot)
	{
		slot = new IceCastSlot;
		memset(slot, 0, sizeof(IceCastSlot));
		STR_CPY(slot->provider_name, "DEBUGGER");
		slot->id = 0;
		slot->testmode = 1;
	}
#endif

	return slot;
}
